/*
 Puzzle JS API.
 Depends on Prototype and scriptaclous
 */
var DUMMY_DB = ['É tarde para economia quando a bolsa está vazia', 'A conselho amigo não feches o postigo', 'Não há maior amigo do que Julho com seu trigo', 'Defeitos do meu amigo lamento mas não maldigo', 'Em tempo de Figos não há amigos'];
'Em tempo de guerra todo o buraco é trincheira', 'Em tempo de guerra não se limpam as armas', 'Não há guerra de mais aparato do que muitas mãos no mesmo prato', 'Quando se declara a guerra o Diabo alarga o Inferno', 'Só para testar o ç cá vai'


var DEFAULT_TIME = 60; //seconds
var DEFAULT_CONTAINER_ELEMENT = 'puzzle';

var PUZZLE_TIMER_CONTAINER_ID = 'puzzle-time-container';
var PUZZLE_TIMER_TITLE_ID = 'puzzle-time-title';
var PUZZLE_TIMER_COUNTER_ID = 'puzzle-time-counter';

var PUZZLE_START_STOP_LINK_ID = 'puzzle-start-stop-link';
var PUZZLE_KEYBOARD_CONTAINER_ID = 'puzzle-keyboard';
var PUZZLE_BOARD_CONTAINER_ID = 'puzzle-container';


var PUZZLE_MISSES_CONTAINER_ID = 'puzzle-tentatives-container';
var PUZZLE_MISSES_TITLE_ID = 'puzzle-tentatives-title';
var PUZZLE_MISSES_ID = 'puzzle-tentatives';
var PUZZLE_MENU_OPTIONS_CONTAINER_ID = 'puzzle-menu-options-container';
var PUZZLE_SPLASH_CONTAINER_ID = 'puzzle-splash-container';
var PUZZLE_SPLASH_RESULT_ID = 'puzzle-splash-result';
var PUZZLE_SPLASH_SOLUTION_TITLE = 'puzzle-splash-result-solution-title';
var PUZZLE_SPLASH_SOLUTION = 'puzzle-splash-solution';
var PUZZLE_MENU_OPTIONS_CONTAINER_ID = 'puzzle-menu-options-container'

var LOOSER_IMAGE_SRC = '../images/sad-icon.png';
var WINER_IMAGE_SRC = '../images/grin-icon.png';


//var START_IMAGE_SRC = '../images/pencil.png';
//var DEFAULT_MAX_TENTATIVES = 5;
//var POINTS_MULTIPLIER = 10;

var DimensionsAdjuster = {
    adjustDimensions: function(){
        //puzzleInnerDivHeigh = $(PUZZLE_TIMER_CONTAINER_ID).getHeight()+$(PUZZLE_KEYBOARD_CONTAINER_ID).getHeight()+$(PUZZLE_BOARD_CONTAINER_ID).getHeight()+$(PUZZLE_MISSES_CONTAINER_ID).getHeight();
        //console.log("puzzleInnerDivHeigh "+puzzleInnerDivHeigh);
        if (true) 
            return;
        $(DEFAULT_CONTAINER_ELEMENT).style.height = '';
        puzzleHeigh = $(DEFAULT_CONTAINER_ELEMENT).getHeight();
        splashHeigh = 0;
        if ($(PUZZLE_SPLASH_CONTAINER_ID)) {
            splashHeigh = $(PUZZLE_SPLASH_CONTAINER_ID).getHeight();
        }
        if (puzzleHeigh < splashHeigh) {
            console.log("(Updating puzzle heigh to: " + splashHeigh);
            //$(DEFAULT_CONTAINER_ELEMENT).setStyle("height:"+splashHeight);
            $(DEFAULT_CONTAINER_ELEMENT).style.height = splashHeigh + 'px';
            //$(DEFAULT_CONTAINER_ELEMENT).style.height='';
        }
        else {
            $(DEFAULT_CONTAINER_ELEMENT).style.height = splashHeigh + 'px';
        }
        console.log("puzzle heigh: " + puzzleHeigh);
        console.log("splashHeigh: " + splashHeigh);
        console.log("new puzzle h: " + $(DEFAULT_CONTAINER_ELEMENT).getHeight());
        console.log("Outer height: " + $('outter-puzzle').getHeight());
        
        
    }
    
}
/*----------------------------------------------------
 Multi-Lang support
 ----------------------------------------------------- */
var i18n = {
    t: function(key, theargs){
        if (!MSGS) {
            return "no lang def;"
        }
        argsH = new $H(theargs);
        text = MSGS[key] || "Key not defined: " + key;
        if (argsH != null) {
            keys = argsH.keys();
            for (i = 0; i < keys.length; i++) {
                text = text.replace("{" + keys[i] + "}", theargs[keys[i]]);
                
            }
        }
        
        
        return text;
    }
}


/*----------------------------------------------------
 PuzzleInformation
 ----------------------------------------------------- */
function PuzzleInformation(word, time, containerElement, maxTentatives){
    this.word = word;
    this.time = time || DEFAULT_TIME;
    this.containerElement = containerElement || DEFAULT_CONTAINER_ELEMENT;
    this.maxTentatives = maxTentatives || DEFAULT_MAX_TENTATIVES;
};


/*----------------------------------------------------
 Puzzle Board
 ----------------------------------------------------- */
function PuzzleBoard(parentElementId, elementId, word){
    this.parentElementId = parentElementId;
    this.elementId = elementId || PUZZLE_BOARD_CONTAINER_ID;
    slots = new Hash();
    this.callbacks = new Array();
    
    words = new Array();
    this.addFinishCallback = function(callback){
        this.callbacks[this.callbacks.length] = callback;
    }
    
    this.render = function(){
        var puzzleElement = new Element('div', {
            'id': this.elementId
        });
        
        var line = new Element('div', {
            'class': 'puzzle-line'
        });
        slotCounter = 0;
        for (i = 0; i < words.length; i++) {
            wordElement = new Element('div');
            for (j = 0; j < words[i].length; j++) {
                currentSlotId = 'slot_' + slotCounter;
                
                
                if (words[i][j] == "-") {
                    var slotEl = new Element('span', {
                        'id': currentSlotId
                    }).update("-");
                    
                    slotEl.addClassName('unguessable');
                    
                }
                else {
                    slots.set(currentSlotId, words[i][j]);
                    var slotEl = new Element('span', {
                        'id': currentSlotId
                    }).update("?");
                    
                }
                
                wordElement.appendChild(slotEl);
                slotCounter++;
            }
            line.appendChild(wordElement);
            if (i < words.length) {
                spacer = new Element('span', {
                    'class': 'spacer'
                });
                line.appendChild(spacer);
            }
            
            
        }
        
        puzzleElement.appendChild(line);
        $(this.parentElementId).appendChild(puzzleElement);
    };
    
    this.completeBoard = function(){
    
        slots.each(function(pair){
            $(pair.key).addClassName('miss');
            $(pair.key).update(pair.value);
            $(pair.key).appear({
                from: 0.7,
                to: 1,
                duration: 0.7
            });
        });
        
        
    };
    this.show = function(){
        $(this.elementId).show()
    };
    
    this.hide = function(){
        $(this.elementId).hide()
    };
    
    specialCharEquivalent = function(actualChar, targetChar){
        //console.log('Checking actualChar ' + actualChar + ' against: ' + targetChar)
        if ((actualChar == 'É' ||
        actualChar == 'È' ||
        actualChar == 'Ê' ||
        actualChar == 'Ẽ') &&
        targetChar == 'E') {
            return true;
        }
        
        if ((actualChar == 'Á' ||
        actualChar == 'À' ||
        actualChar == 'Ã' ||
        actualChar == 'Â') &&
        targetChar == 'A') {
            return true;
        }
        
        if ((actualChar == 'Ó' ||
        actualChar == 'Ò' ||
        actualChar == 'Ô' ||
        actualChar == 'Õ') &&
        targetChar == 'O') {
            return true;
        }
        
        if ((actualChar == 'Í' ||
        actualChar == 'Ì' ||
        actualChar == 'Î' ||
        actualChar == 'Ĩ') &&
        targetChar == 'I') {
            return true;
        }
        
        if ((actualChar == 'Ú' ||
        actualChar == 'Ù' ||
        actualChar == 'Û' ||
        actualChar == 'Ũ') &&
        targetChar == 'U') {
            return true;
        }
        
        if ((actualChar == 'Ç') &&
        targetChar == 'C') {
            return true;
        }
        
        
        
        //ÉÈÃÁÀÇ
    };
    this.play = function(code){
        hits = 0;
        char = String.fromCharCode(code);
        console.log("Playing: " + char);
        foundKeys = new Array();
        slots.each(function(pair){
            //console.log("Key: "+pair.key);
            //console.log("Value: "+pair.value);
            
            if (pair.value == char || specialCharEquivalent(pair.value, char)) {
                hits++
                
                $(pair.key).addClassName('hit');
                $(pair.key).update(pair.value);
                $(pair.key).appear({
                    from: 0.7,
                    to: 1,
                    duration: 0.7
                });
                
                
                foundKeys[foundKeys.length] = pair.key;
            }
        });
        
        //remove foundKeys
        for (i = 0; i < foundKeys.length; i++) {
            slots.unset(foundKeys[i]);
            if (slots.keys().length == 0) {
                for (i = 0; i < this.callbacks.length; i++) {
                    this.callbacks[i]();
                }
                
            }
        }
        return hits;
    };
    words = word.toUpperCase().split(' ');
    
    
};


/*----------------------------------------------------
 Puzzle Misses
 ----------------------------------------------------- */
function PuzzleMisses(parentElementId, elementId, maxTentatives){
    this.elementId = elementId;
    this.parentElementId = parentElementId;
    this.maxTentatives = maxTentatives;
    this.misses = 0;
    this.callbacks = new Array();
    
    this.getFinalScore = function(){
        return (this.maxTentatives - this.misses) * POINTS_MULTIPLIER;
    }
    
    
    
    this.addMaxMissesCallback = function(callback){
        this.callbacks[this.callbacks.length] = callback;
    }
    
    this.render = function(){
        var element = new Element('div', {
            'id': elementId
        });
        var title = new Element('span', {
            'id': PUZZLE_MISSES_TITLE_ID
        }).update(i18n.t('misses'));
        var misses = new Element('span', {
            'id': PUZZLE_MISSES_ID
        }).update(this.maxTentatives - this.misses);
        element.appendChild(title);
        element.appendChild(misses);
        $(this.parentElementId).appendChild(element)
    };
    
    this.show = function(){
        $(this.elementId).show();
    };
    this.updateMisses = function(){
        this.misses++;
        $(PUZZLE_MISSES_ID).update(this.maxTentatives - this.misses);
        $(PUZZLE_MISSES_ID).pulsate({
            pulses: 3,
            duration: 0.5
        });
        $(PUZZLE_MISSES_ID).highlight();
        
        
        if (this.maxTentatives - this.misses == 0) {
            for (i = 0; i < this.callbacks.length; i++) {
                this.callbacks[i]();
            }
        }
    }
    
    this.hide = function(){
        $(this.elementId).hide();
    }
};


function PuzzleButton(parentElementId, buttonId, label, callback){
    this.parentElementId = parentElementId;
    this.buttonId = buttonId;
    this.label = label;
    this.callback = callback;
    
    this.render = function(){
        aEl = new Element('a', {
            'id': this.buttonId,
            'href': 'javascript:void(0)'
        }).update(this.label);
        $(this.parentElementId).appendChild(aEl);
        aEl.observe('click', callback);
    }
};


function PuzzleMenuOptions(parentElementId, elementId, callbacks){
    this.parentElementId = parentElementId;
    this.elementId = elementId;
    this.callbacks = callbacks;
    
    this.render = function(){
        var element = new Element('div', {
            'id': this.elementId
        });
        $(this.parentElementId).appendChild(element);
        instance = this;
        var btNewGame = new PuzzleButton(this.elementId, "puzzle-new-game-btn", i18n.t('play-again'), function(){
            if (instance.callbacks && instance.callbacks['play-again']) {
            
                instance.callbacks['play-again']();
            }
        });
        
        btNewGame.render();
        if (show_share_with_friends_btn) {
            var btShareResults = new PuzzleButton(this.elementId, "puzzle-share-results-btn", i18n.t('share-results'), function(){
                if (instance.callbacks && instance.callbacks['share-results']) {
                
                    instance.callbacks['share-results']();
                }
            });
            btShareResults.render();
        }
        
        
        
        
        var btinviteFriends = new PuzzleButton(this.elementId, "puzzle-invite-friends-btn", i18n.t('invite-friends'), function(){
            if (instance.callbacks && instance.callbacks['invite-friends']) {
            
                instance.callbacks['invite-friends']();
            }
        });
        btinviteFriends.render();
        
        
        
        //btNewGame.src = BUTTON_IMG_SRC;
        //element.appendChild(btNewGame);
        //$(this.parentElementId).appendChild(element);
    
    };
};
/*----------------------------------------------------
 Puzzle KeyBoard
 ----------------------------------------------------- */
function PuzzleKeyboard(parentElementId, elementId){
    this.parentElementId = parentElementId;
    this.elementId = elementId;
    this.callbacks = new Array();
    instance = this;
    
    
    this.render = function(){
        var mainKeyboardContainer = new Element('div', {
            'id': this.elementId
        });
        var keyboardLine1 = new Element('div', {
            'class': 'puzzle-keybord-line'
        });
        var keyboardLine2 = new Element('div', {
            'class': 'puzzle-keybord-line'
        });
        
        
        
        for (i = 65; i < 91; i++) {
            keyId = "key_" + i;
            key = new Element('a', {
                'id': keyId,
                'href': 'javascript:this.onClick(' + i + ')'
            }).update(String.fromCharCode(i));
            
            
            if (i < 78) {
                keyboardLine1.appendChild(key);
            }
            else {
                keyboardLine2.appendChild(key);
            }
        };
        mainKeyboardContainer.appendChild(keyboardLine1)
        mainKeyboardContainer.appendChild(keyboardLine2)
        
        $(this.parentElementId).appendChild(mainKeyboardContainer);
    };
    
    this.show = function(){
        $(this.elementId).show();
    };
    
    
    this.hide = function(){
        $(this.elementId).hide();
    }
    this.addCallback = function(fnt){
        this.callbacks[this.callbacks.length] = fnt;
    };
    
    this.hideKey = function(code){
        $('key_' + code).update("&nbsp;");
        $('key_' + code).setOpacity(0.5);
        $('key_' + code).href = "javascript:void(0)";
        
    };
    onClick = function(code){
    
        for (i = 0; i < instance.callbacks.length; i++) {
            instance.callbacks[i](code);
            
        }
    }
    
};

/*----------------------------------------------------
 Puzzle Timer
 ----------------------------------------------------- */
function PuzzleTimer(parentElementId, elementId){
    this.elementId = elementId || PUZZLE_TIMER_CONTAINER_ID;
    this.parentElementId = parentElementId;
    
    elapsedSeconds = 0;
    elapsedMinutes = 0;
    elapsedHours = 0;
    totalElapsedSeconds = 0;
    timerCounter = null;
    instance = this;
    
    
    this.getFinalSeconds = function(){
        return elapsedSeconds;
    };
    this.getFinalHours = function(){
        return elapsedHours;
    };
    this.getFinalMinutes = function(){
        return elapsedMinutes;
    };
    this.hide = function(){
        $(this.elementId).hide();
    };
    
    this.show = function(){
        $(this.elementId).show();
    };
    
    getTimerCountLabel = function(){
        seconds = (elapsedSeconds < 10 ? "0" : "") + elapsedSeconds;
        minutes = (elapsedMinutes < 10 ? "0" : "") + elapsedMinutes;
        hours = (elapsedHours < 10 ? "0" : "") + elapsedHours;
        
        label = hours + ":" + minutes + ":" + seconds;
        return label;
    };
    
    updateTimer = function(){
        totalElapsedSeconds++;
        elapsedSeconds++;
        if (elapsedSeconds == 60) {
            elapsedMinutes++;
            elapsedSeconds = 0;
        }
        if (elapsedMinutes == 60) {
            elapsedMinutes = 0;
            elapsedHours++;
        }
        $(PUZZLE_TIMER_COUNTER_ID).update(getTimerCountLabel());
    };
    
    this.render = function(){
    
        var timerElement = new Element("div", {
            'id': this.elementId
        });
        
        var timerTitleElement = new Element('span', {
            'id': this.elementId
        }).update(i18n.t('time'));
        var timerCounterElement = new Element('span', {
            'id': PUZZLE_TIMER_COUNTER_ID
        }).update(getTimerCountLabel());
        
        timerElement.appendChild(timerTitleElement);
        timerElement.appendChild(timerCounterElement);
        
        $(this.parentElementId).appendChild(timerElement);
    };
    
    this.start = function(){
        this.timerCounter = new PeriodicalExecuter(function(){
            updateTimer()
        }, 1);
    };
    this.stop = function(){
        if (this.timerCounter != null) {
            this.timerCounter.stop();
        }
        
        this.timerCounter = null;
        //elapsedSeconds = 0;
        //elapsedMinutes = 0;
        //elapsedHours = 0;
        //totalElapsedSeconds = 0;
        //Reset to 00:00:00
        //$(PUZZLE_TIMER_COUNTER_ID).update(getTimerCountLabel());
    };
    
    
}

function PuzzleSplash(parentElementId, elementId, puzzleInfo, callbacks){
    this.puzzleInfo = puzzleInfo;
    this.parentElementId = parentElementId;
    this.elementId = elementId;
    this.callbacks = callbacks;
    
    this.reset = function(){
        $(this.elementId).update("");
    };
    this.render = function(){
        element = new Element('div', {
            'id': elementId
        });
        $(parentElementId).appendChild(element);
        element.setStyle('display:none');
    };
    
    this.hide = function(){
    
        $(this.elementId).hide();
        
    };
    
    this.show = function(){
        $(this.elementId).appear({
            duration: 2.0,
            to: 0.95
        });
    };
    
    this.showWinnerScreen = function(points, hours, minutes, seconds){
        this.show();
        console.log("Points: " + points);
        console.log("Seconds: " + seconds);
        console.log("Minutes: " + minutes);
        console.log("hours: " + hours);
        
        
        winnerImage = new Image();
        winnerImage.src = WINER_IMAGE_SRC;
        $(this.elementId).appendChild(winnerImage);
        
        
        text = i18n.t('you-win-seconds', {
            'points': points,
            'seconds': seconds
        });
        
        if (minutes > 1) {
            text = i18n.t('you-win-minutes', {
                'points': points,
                'minutes': minutes,
                'seconds': seconds
            });
        }
        
        if (minutes == 1) {
            text = i18n.t('you-win-minute', {
                'points': points,
                'minute': minutes,
                'seconds': seconds
            });
        }
        
        
        if (hours > 0) {
            text = i18n.t('you-win-hours', {
                'points': points,
                'hours': hours,
                'minutes': minutes,
                'seconds': seconds
            });
        }
        
        
        
        winnerInfoEl = new Element('div', {
            'id': PUZZLE_SPLASH_RESULT_ID
        }).update(text);
        
        $(this.elementId).appendChild(winnerInfoEl);
        
        var pzOptions = new PuzzleMenuOptions(this.elementId, PUZZLE_MENU_OPTIONS_CONTAINER_ID, this.callbacks);
        pzOptions.render();
        
        //$(this.elementId).height = $(parentElementId).height;
        //$(this.elementId).width = $(parentElementId).width;
    
    };
    
    this.showStartSplash = function(callback){
        //this.show();
        $(this.elementId).appear({
            duration: 2.0,
            to: 1
        });
        looserImage = new Image();
        looserImage.src = START_IMAGE_SRC;
        $(this.elementId).appendChild(looserImage);
        
        
        welcomeInfoEl = new Element('div', {
            'id': 'splash-welcome-text'
        }).update(i18n.t('welcome'));
        
        menuOptions = new Element('div', {
            'id': PUZZLE_MENU_OPTIONS_CONTAINER_ID
        });
        $(this.elementId).appendChild(welcomeInfoEl);
        $(this.elementId).appendChild(menuOptions);
        //button
        var el = this.elementId;
        var instance = this;
        
        var btNewGame = new PuzzleButton(PUZZLE_MENU_OPTIONS_CONTAINER_ID, "puzzle-start-game-btn", i18n.t('start-play'), function(){
            $('puzzle-start-game-btn').remove();
            instance.hide();
            callback();
        });
        
        btNewGame.render();
        
        
    };
    
    this.showLooserScreen = function(){
        this.show();
        
        looserImage = new Image();
        looserImage.src = LOOSER_IMAGE_SRC;
        $(this.elementId).appendChild(looserImage);
        looserInfoEl = new Element('div', {
            'id': PUZZLE_SPLASH_RESULT_ID
        }).update(i18n.t('you-lost'));
        
        $(this.elementId).appendChild(looserInfoEl);
        looserInfoEl.setStyle('padding-top : 0px');
        
        solutionTitleEl = new Element('div', {
            'id': PUZZLE_SPLASH_SOLUTION_TITLE
        }).update(i18n.t('solution-is'));
        $(this.elementId).appendChild(solutionTitleEl);
        
        solutionEL = new Element('div', {
            'id': PUZZLE_SPLASH_SOLUTION
        }).update(puzzleInfo.word.toUpperCase());
        $(this.elementId).appendChild(solutionEL);
        
        var pzOptions = new PuzzleMenuOptions(this.elementId, PUZZLE_MENU_OPTIONS_CONTAINER_ID, this.callbacks);
        pzOptions.render();
        
        $(this.elementId).height = $(parentElementId).height;
        $(this.elementId).width = $(parentElementId).width;
        
        
    };
};

/*----------------------------------------------------
 Puzzle
 ----------------------------------------------------- */
function Puzzle(puzzleInfo, extCallbacks){
    puzzleInfo = puzzleInfo;
    this.points = 0;
    this.hours = 0;
    this.minutes = 0;
    this.seconds = 0;
    
    running = false;
    
    this.extCallbacks = extCallbacks;
    
    
    
    
    puzzleTimer = new PuzzleTimer(puzzleInfo.containerElement, PUZZLE_TIMER_CONTAINER_ID);
    //puzzleStartStopLink = new PuzzleStartStopLink(puzzleInfo.containerElement, PUZZLE_START_STOP_LINK_ID);
    puzzleKeyboard = new PuzzleKeyboard(puzzleInfo.containerElement, PUZZLE_KEYBOARD_CONTAINER_ID);
    puzzleBoard = new PuzzleBoard(puzzleInfo.containerElement, PUZZLE_BOARD_CONTAINER_ID, puzzleInfo.word)
    puzzleMisses = new PuzzleMisses(puzzleInfo.containerElement, PUZZLE_MISSES_CONTAINER_ID, puzzleInfo.maxTentatives);
    puzzleSplash = new PuzzleSplash(puzzleInfo.containerElement, PUZZLE_SPLASH_CONTAINER_ID, puzzleInfo, this.extCallbacks);
    the_instance = this;
    puzzleMisses.addMaxMissesCallback(function(){
        puzzleBoard.completeBoard();
        puzzleTimer.stop();
        puzzleSplash.reset();
        this.points = puzzleMisses.getFinalScore();
        this.seconds = puzzleTimer.getFinalSeconds();
        this.minutes = puzzleTimer.getFinalMinutes();
        this.hours = puzzleTimer.getFinalHours();
        the_instance.hideAll();
        puzzleSplash.showLooserScreen();
        
        if (extCallbacks && extCallbacks['finished']) {
            extCallbacks['finished'](this.points, this.hours, this.minutes, this.seconds);
        }
    });
    
    puzzleBoard.addFinishCallback(function(){
        puzzleTimer.stop();
        this.points = puzzleMisses.getFinalScore();
        this.seconds = puzzleTimer.getFinalSeconds();
        this.minutes = puzzleTimer.getFinalMinutes();
        this.hours = puzzleTimer.getFinalHours();
        
        puzzleSplash.reset();
        the_instance.hideAll();
        puzzleSplash.showWinnerScreen(this.points, this.hours, this.minutes, this.seconds);
        
        if (extCallbacks && extCallbacks['finished']) {
            extCallbacks['finished'](this.points, this.hours, this.minutes, this.seconds);
        }
    });
    
    puzzleKeyboard.addCallback(function(code){
        moves = puzzleBoard.play(code);
        if (moves > 0) {
            //window.alert("Great!");
        }
        else {
        
            puzzleMisses.updateMisses();
        }
        puzzleKeyboard.hideKey(code);
        
    });
    this.hideAll = function(){
    
        puzzleTimer.hide();
        puzzleMisses.hide();
        puzzleBoard.hide();
        puzzleKeyboard.hide();
    };
    
    this.render = function(showStartSplash){
    
        puzzleSplash.render();
        puzzleTimer.render();
        //puzzleTimer.hide();
        puzzleMisses.render();
        //puzzleMisses.hide();
        puzzleBoard.render();
        //puzzleBoard.hide();
        
        puzzleKeyboard.render();
        //puzzleKeyboard.hide();
        this.hideAll();
        if (showStartSplash) {
            puzzleSplash.showStartSplash(function(){
                puzzleTimer.show();
                puzzleKeyboard.show();
                puzzleBoard.show();
                puzzleTimer.start();
                puzzleMisses.show();
                
            });
            
        }
        else {
        
            puzzleTimer.show();
            puzzleKeyboard.show();
            puzzleBoard.show();
            puzzleTimer.start();
            puzzleMisses.show();
            
        }
        
    }
};

